package com.jspgou.cms.manager.impl;

import java.io.IOException;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jspgou.cms.dao.StoreDao;
import com.jspgou.cms.entity.Category;
import com.jspgou.cms.entity.ShopMember;
import com.jspgou.cms.entity.Store;
import com.jspgou.cms.entity.StoreExt;
import com.jspgou.cms.entity.StoreModel;
import com.jspgou.cms.entity.StoreTxt;
import com.jspgou.cms.manager.CategoryMng;
import com.jspgou.cms.manager.FullGiveMng;
import com.jspgou.cms.manager.ResourceMng;
import com.jspgou.cms.manager.ShopMemberMng;
import com.jspgou.cms.manager.StoreExtMng;
import com.jspgou.cms.manager.StoreMng;
import com.jspgou.cms.manager.StoreModelMng;
import com.jspgou.cms.manager.StoreTxtMng;
import com.jspgou.common.hibernate3.Updater;
import com.jspgou.common.page.Pagination;
import com.jspgou.core.entity.Website;

@Service
@Transactional
public class StoreMngImpl implements StoreMng {
	@Transactional(readOnly = true)
	public Pagination getPage(int pageNo, int pageSize) {
		Pagination page = dao.getPage(pageNo, pageSize);
		return page;
	}
	
	@Transactional(readOnly = true)
	public Pagination getPage(Long ctgId,int pageNo, int pageSize) {
		Pagination page = dao.getPage(ctgId,pageNo, pageSize);
		return page;
	}
	
	public Pagination getPage(Long ctgId,Boolean examine,int pageNo, int pageSize){
		Pagination page = dao.getPage(ctgId,examine,pageNo, pageSize);
		return page;
	}
	
	public Pagination getPage(Long ctgId,Boolean examine,Boolean forbidden,
			int pageNo, int pageSize){
		Pagination page = dao.getPage(ctgId,examine,forbidden,pageNo, pageSize);
		return page;
	}

	@Transactional(readOnly = true)
	public Store findById(Long id) {
		Store entity = dao.findById(id);
		return entity;
	}
	
	public Store findByStorePath(String storePath){
		Store entity=dao.findByStorePath(storePath);
		return entity;
	}
	
	public Store findByDomainName(String domainName){
		Store entity=dao.findByDomainName(domainName);
		return entity;
	}

	public Store save(Store bean) {
		dao.save(bean);
		return bean;
	}
	public Store register(Store bean,StoreExt ext,StoreTxt txt,Long memberId,
			String domainPreFix,Website web,Long categoryId,boolean examine) throws IOException{
		Category category=categoryMng.findById(categoryId);
		ShopMember member=shopMemberMng.findById(memberId);
		bean.setPriority(10);
		bean.setRecommend(false);
		bean.setForbidden(false);
		bean.setCategory(category);
		bean.setSortDate(new Date());
		bean.setCreateDate(new Date());
		bean.setExamine(examine);
		bean.setWebsite(web);
		bean.setStoreModel(storeModelMng.getByDefaultModel(true));
		bean.setModelpermission(false);
		bean.setMember(member);
		//设置模板
		StoreModel sm=storeModelMng.getByDefaultModel(true);
		StoreModel sm1=storeModelMng.getByDefaultModel(true);
		if(sm==null){
			sm=storeModelMng.getList().get(0);
			sm1 = storeModelMng.getList().get(1);
		}
		bean.setStoreModel(sm);
	    bean=dao.save(bean);
		//设计店铺域名
		Long id=bean.getId()+10000;
		String code=id.toString().substring(1);
		String domain=domainPreFix+code+"."+web.getBaseDomain();
		ext.setStorePath(domainPreFix+code);
		ext.setCode(code);
		ext.setDomain(domain);
		storeExtMng.save(ext, bean);
		storeTxtMng.save(txt, bean);
		resourceMng.copyStoreTplAndRes(sm.getPath(),sm1.getPath(),domainPreFix+code);
		//会员表中加入店铺
		member.setStore(bean);
		shopMemberMng.update(member);
		//加入店主
	     bean.addToMembers(member);
		return bean;
	}

	public Store update(Store bean) {
		Updater<Store> updater = new Updater<Store>(bean);
		Store entity = dao.updateByUpdater(updater);
		return entity;
	}
	
	public Store update(Store bean,Long[] fullGiveIds) {
		Updater<Store> updater = new Updater<Store>(bean);
		Store entity = dao.updateByUpdater(updater);
		// 更新满就送
		entity.getFullGives().clear();
		if (fullGiveIds != null && fullGiveIds.length > 0) {
			for (int i = 0, len = fullGiveIds.length; i < len; i++) {
				if (fullGiveIds[i]!=null) {
					entity.addToFullGives(fullGiveMng.findById(fullGiveIds[i]));
				}
			}
		}
		return entity;
	}
	
	public Store updateAdm(Store bean,StoreExt ext,StoreTxt txt,Long categoryId,Long storeModelId) {
		Store store = findById(bean.getId());
		Category category=categoryMng.findById(categoryId);
		bean.setCategory(category);
//		StoreModel storeModel=storeModelMng.findById(storeModelId);
//		bean.setStoreModel(storeModel);
		
		// 更新主表
		Updater<Store> updater = new Updater<Store>(bean);
		bean = dao.updateByUpdater(updater);
		// 更新扩展表
		storeExtMng.update(ext);
		// 更新文本表
		storeTxtMng.update(txt, bean);
		return store;
	}
	
	public Store updateFro(Store bean,StoreExt ext,StoreTxt txt,
			Long categoryId,Long storeModelId){
		Store store = findById(bean.getId());
		Category category=categoryMng.findById(categoryId);
		if(category!=null){
			bean.setCategory(category);
			StoreModel storeModel=storeModelMng.findById(storeModelId);
			bean.setStoreModel(storeModel);
			// 更新主表
			Updater<Store> updater = new Updater<Store>(bean);
			bean = dao.updateByUpdater(updater);
			// 更新扩展表
			storeExtMng.update(ext);
			// 更新文本表
			storeTxtMng.update(txt, bean);
		}
		return store;
	}

	public Store deleteById(Long id) {
		Store bean = dao.deleteById(id);
		return bean;
	}
	
	public Store[] deleteByIds(Long[] ids) {
		Store[] beans = new Store[ids.length];
		for (int i = 0,len = ids.length; i < len; i++) {
			beans[i] = deleteById(ids[i]);
		}
		return beans;
	}

	private StoreDao dao;
	@Autowired
	private StoreExtMng storeExtMng;
	@Autowired
	private StoreTxtMng storeTxtMng;
	@Autowired
	private CategoryMng categoryMng;
	@Autowired
	private StoreModelMng storeModelMng;
	@Autowired
	private ShopMemberMng shopMemberMng;
	@Autowired
	private ResourceMng resourceMng;
	@Autowired
	private FullGiveMng fullGiveMng;

	@Autowired
	public void setDao(StoreDao dao) {
		this.dao = dao;
	}
}